<?php
class Getmetrics extends Controller {
	
		function __construct()
			{
				parent::Controller();	
				$this->is_logged_in(); 
			}
		function is_logged_in() //check for session 
		{
			$is_logged_in = $this->session->userdata('is_logged_in');
			if(!isset($is_logged_in) || $is_logged_in != true)
			{
				echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';	
				die();		
				//$this->load->view('login_form');
			}		
		}
		function pie_graph()
		{
			$this->load->view('pie');
		}
		function tracking_graph()
		{
			$this->load->view('tracking');
		}
		function general_status()
	{
		//load general statistic - 
		$this->load->view('status');
	}
		//Grab the multidimensional array and process it.
		//
		function view_metrics($servername)
		{
			$metrics=$this->metrics_model->get_metrics($servername);
			$field = $this->metrics_model->get_table_field($servername);
			//begin to cook data - isolate datatime first
			for ($i=0;$i<sizeof($metrics);$i++)
			{
				$date[$i]=($metrics[$i][0])*1000; //FLOT time is in GMT and mil-sec
			}
			//this loop-in-loop might be hard to understand!!
			//the first FOR is loop through N performance-counters. IGNORE the first field as it is datetime. 
			//view class will accept associative array. This outer loop will dynamically generate an associative array
			//based on N performance-counters (big changes compared to getdata_unused.php, line 23 to 53
			//where $contents are specified MANUALLY. 
			for ($i=1;$i<sizeof($field);$i++)
			{
				$string="$field[$i]";
				//the second loop is to isolate each performance counters) 
				for ($j=0;$j<sizeof($metrics);$j++)
				{
					$pcounter[$j]=$metrics[$j][$i];
				}
				
				$content[$string]=$this->cook_data($date,$pcounter);
			}
			$content['servername']=$servername;
			if (preg_match('/mysql$/',$servername))
			{
				$this->load->view('graphsql',$content);
			}
			else
			{
				switch ($servername) {
					case "raid5server":
						$this->load->view('graph5',$content);
						break;
					case "raid6server":
						$this->load->view('graph6',$content);
						break;
					case "rackMaster":
						$this->load->view('graphnix',$content);
						break;
					case "ds1geodata":
					case "ds2geodata":
						$this->load->view('graphnix',$content);
						break;
					default:
						$this->load->view('graph',$content);
						break;
				}
			}
		}
			
		function cook_data($array1,$array2) 
		{
		//create 2D array, return json
			$i=count($array1);
			for ($j=0;$j < $i; $j++) 
			{
				$finalArray[$j]=array($array1[$j],$array2[$j]);
			}
			return json_encode($finalArray);
		}
		//generate parameters to create view
		function cook_view()
		{
			$content['limit'] = 19;
			$this->load->view('graph',$content);
		}
}
?>
